//
//  HZSearchView.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/16.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit

class HZSearchView: UIView {

 
    @IBOutlet weak var titleFind: UITextField!
    
    @IBOutlet weak var rightMargin: NSLayoutConstraint!
    /**
     类方法创建控制器
     */
    class func setSearch() -> HZSearchView {
        
        return NSBundle.mainBundle().loadNibNamed("HZSearchView", owner: nil, options: nil).last as! HZSearchView
        
    }
    
    
    //视图nib 加载完成之后
    override func awakeFromNib() {
        
        super.awakeFromNib()
        
      
        
        
        titleFind.leftView = img
        
        //设置编辑框的左视图大小
       titleFind.leftView?.frame.size = CGSize(width: self.frame.height, height: self.frame.height)
        
        //一定要设置编辑框的左视图总显示,要不然会不显示的***
        titleFind.leftViewMode = .Always
        
        
//        titleFind.layer.cornerRadius = 5
//        titleFind.layer.masksToBounds = true
//        titleFind.layer.borderColor = UIColor.redColor().CGColor
//        titleFind.layer.borderWidth = 3
        
    }
    
    
    
    
  //点击按钮取消编辑框
    @IBAction func cancel(sender: AnyObject) {
        
        
        //约束动画
        rightMargin.constant = 0
        
        //动画
        UIView.animateWithDuration(0.3) { () -> Void in
            
            self.layoutIfNeeded()
        }
        
        //退出编辑框的第一响应者
        titleFind.resignFirstResponder()
        

    }
    
    //点击编辑框
    @IBAction func EditingBegin(sender: UITextField) {
        
        
        //约束动画
        rightMargin.constant = 45
        
        //动画
        UIView.animateWithDuration(0.3) { () -> Void in
            
            self.layoutIfNeeded()
        }
        
        
    }
    
    
    //懒加载编辑框的左视图
    lazy var img:UIImageView = {
        
        
        let img = UIImageView()
        
        img.image = UIImage(named: "searchbar_textfield_search_icon")
        
        //设置控件内的内容区中显示
        img.contentMode = .Center
        
        return img
        
    }()
    
    

}



